/*
 * DiscoveryWantToBuyPresenter.java
 * business
 *
 * Created by chentao on 2017/5/13.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.discovery.page.wanttobuy;

import com.yidiandao.business.data.model.DiscoveryModel;
import com.yidiandao.business.data.model.IDiscoveryModel;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.List;

/**
 * 发现-求购
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/5/13 0013 9:24
 */
public class DiscoveryWantToBuyPagePresenter implements DiscoveryWantToBuyPageContact.Presenter {

    private DiscoveryWantToBuyPageContact.View mWantToBuyView;

    private final IDiscoveryModel mDiscoveryModel;

    public DiscoveryWantToBuyPagePresenter(DiscoveryWantToBuyPageContact.View view) {
        mWantToBuyView = view;
        mWantToBuyView.setPresenter(this);

        mDiscoveryModel = new DiscoveryModel();
    }

    @Override
    public void start() {
        loadData(true);
    }

    @Override
    public void loadData(final boolean isRefreshing) {

        mDiscoveryModel.loadWantToBuyPageData(isRefreshing, mWantToBuyView.getPagerTag(), new IDiscoveryModel.OnLoadDiscoveryListener<List<Object>>() {
            @Override
            public void onSuccess(List<Object> objects, boolean emptyResult, boolean hasMore) {
                if (isRefreshing) {
                    mWantToBuyView.refreshContent(mWantToBuyView.checkResultObject(objects));
                    mWantToBuyView.setRefreshComplete();
                }

                mWantToBuyView.loadMoreFinish(emptyResult, hasMore);
                mWantToBuyView.setPageData(objects);
            }

            @Override
            public void onFailed() {
                if (isRefreshing) {
                    mWantToBuyView.refreshContent(SuperLoadingLayout.STATE_ERROR);
                } else {
                    mWantToBuyView.loadMoreError();
                }
            }

            @Override
            public void onFinal() {

            }
        });
    }
}
